package net.zabuchy.user.dao;
 
import java.util.Formatter;
import java.util.List;
 
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.util.DigestUtils;
 
import net.zabuchy.UserRole.entity.User_roles;
import net.zabuchy.helper.PasswordHelper;
import net.zabuchy.issue.dao.IssueDao;
import net.zabuchy.loginHelper.UserRoles;
import net.zabuchy.user.action.UserAction;
import net.zabuchy.user.entity.User;
 
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
 
	@Autowired
	IssueDao issueDao;
	
	
	private static final Logger LOGGER = Logger.getLogger(UserAction.class.getName());
	
    public void add(User user) {
    	
    	String decodedPassword = PasswordHelper.encodePassword(user.getPassword());
    	
    	user.setPassword(decodedPassword);
    	
        getHibernateTemplate().save(user);
    }
 
    public void delete(User user) {
        getHibernateTemplate().delete(user);
    }
 
    public List findAll() {
        return getHibernateTemplate().find("from User");
    }
 
    public User findById(int id) {
        return (User) getHibernateTemplate().find("from User where USER_ID=?", id).get(0);
    }
    
    public List findByCustomerId(int id) {
        return  getHibernateTemplate().find("from User where CUSTOMER_ID=?", id);
    }
 
    public User findByUsername(String login) {
    	User user = (User) getHibernateTemplate().find("from User where username=?", login).get(0);
        return user;
    }
 
    public void update(User user) {
        getHibernateTemplate().update(user);
    }
    
    public String getUserRole(User user) {
    	if(user == null) {
    		return null;
    	}
    	Session session = getSession();
    	String hql = "from User_roles roles where roles.userId = '" + user.getUserId() + "'";
    	List resultList = session.createQuery(hql).list();
    	User_roles role = (User_roles) resultList.get(0);
    	LOGGER.debug("rola: " + role.getAuthority());
    	return role.getAuthority();
    }
    
    public void addUserRole(String role, int userId){
    	User_roles newEntry = new User_roles(role, userId);
    	getHibernateTemplate().save(newEntry);
    	LOGGER.debug("added role: " + role + " to userId: " + userId);
    }
    
    public void updateUserRole(User user, String role) {
    	Session session = getSession();
    	String hql = "update User_roles set authority = :newRole where userId = :userId";
    	int updatedEntities = session.createQuery( hql )
        .setString( "newRole", role )
        .setString( "userId", String.valueOf(user.getUserId()))
        .executeUpdate();
    }
    
    public List<User_roles> findUserRoles() {
    	return getHibernateTemplate().find("from User_roles");
    }

	public IssueDao getIssueDao() {
		return issueDao;
	}

	public void setIssueDao(IssueDao issueDao) {
		this.issueDao = issueDao;
	}
}